//
//  MegatlonTests.m
//  MegatlonTests
//
//  Created by Leandro Milmanda on 4/7/12.
//  Copyright 2012 www.lemil.com.ar. All rights reserved.
//

#import "MegatlonTests.h"
#import "JSON.h"
#import "SucursalesDao.h";

#define kiMegatlonServiceUrl @"http://www.lemil.com.ar/apps/imegatlon/sucursales.php"


@implementation MegatlonTests

@synthesize responseData;

- (void)setUp
{
    [super setUp];
    
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}


-(void)testMegatlonService
{
    NSData *dataReply = nil;
    NSString *stringReply = nil;
    NSURLRequest *request = nil;
    NSURLResponse *response = nil;
    NSError *error = nil;
    
    request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:kiMegatlonServiceUrl]];  
    [request setHTTPMethod: @"GET"];  
    [request setHTTPBody: [[NSString stringWithString:@""] dataUsingEncoding: NSUTF8StringEncoding]];  
    
    dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
    
    stringReply = [[[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding] autorelease];  

    NSString *retval = [self parseResponse: stringReply];
    
    NSLog(retval);
    
    STAssertTrue(retval != nil, @"ok");
    

}

- (NSString *)parseResponse:(NSString *)someResponseString {
    
    NSArray* todasLasSucursales = [(NSDictionary*)[someResponseString JSONValue] objectForKey:@"sucursales"];

	//choose a random sucursal
	NSDictionary* unaSucursal = [todasLasSucursales objectAtIndex:0];
	
	NSNumber* estado = [unaSucursal objectForKey:@"estado"];    
	NSString* nombre = [unaSucursal objectForKey:@"nombre"];
	NSString* horario = [unaSucursal objectForKey:@"horario"] ;
	NSString* direccion = [unaSucursal objectForKey:@"direccion"] ;
	    
	return [NSString stringWithFormat:@"Sucursal %@ , Dirección: %@ ,  horario %@,  estado: %@",
                  nombre,direccion,horario, estado
                  ];
}


-(void)testSucursalService 
{
    SucursalesDao *dao = [SucursalesDao instance];
    [dao load];
    
    STAssertTrue([dao isLoaded], @"Dao is Loaded!");
    
    
    BOOL shoudlBeNo = [dao isOpenedSucursalId:0 atDayNumber:1 andTime:7 andMinutes:45];
    
    STAssertFalse(shoudlBeNo, @"This should be false");
    
    BOOL shoudlBeYes = [dao isOpenedSucursalId:0 atDayNumber:1 andTime:9 andMinutes:45];
    
    STAssertTrue(shoudlBeYes, @"This should be true");
    
}


@end
